package com.example.framelib.mvp.frame;

import android.os.Bundle;

import com.example.framelib.mvp.frame.base.BaseView;
import com.example.framelib.ui.fragment.BaseFragment;

import java.util.Random;


public abstract class MvpFragment<P extends FragmentPresenter> extends BaseFragment {

    protected P mPresenter;
    private String presenterKey;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        presenterKey =String.valueOf(System.currentTimeMillis()+new Random().nextInt());
        mPresenter = (P) Mvp.getInstance().getPresenter(presenterKey,Mvp.getGenericType(this, 0));
        mPresenter.attachView(getBaseView());
    }

    protected abstract BaseView getBaseView();

    @Override
    public void onResume() {
        super.onResume();
        if (mPresenter != null){
            mPresenter.onResume();
        }
    }

    @Override
    public void onPause() {
        super.onPause();
        if (mPresenter != null){
            mPresenter.onPause();
        }
    }


    @Override
    protected void onUserInvisible() {
        if (mPresenter != null){
            mPresenter.onUserInvisible();
        }
    }

    @Override
    protected void onUserVisible() {
        if (mPresenter != null){
            mPresenter.onUserVisible();
        }
    }

    @Override
    protected void onFirstUserVisible() {
        if (mPresenter != null){
            mPresenter.onFirstUserVisible();
        }
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mPresenter != null){
            mPresenter.onDestroy();
        }
    }
}
